package com.wxzz.elearing.course.api.app;

import com.alibaba.fastjson.JSONObject;
import com.wxzz.elearing.course.dto.CurriculumSectionTopicIsPassDto;
import com.wxzz.elearing.course.dto.ExamIsPassDto;
import com.wxzz.elearing.course.dto.WxUserExamHistoryDto;
import com.wxzz.elearing.course.entity.CurriculumSectionTopic;
import com.wxzz.elearing.course.entity.WxUserCurriculumSection;
import com.wxzz.elearing.course.entity.WxUserExamHistory;
import com.wxzz.elearing.course.service.ICurriculumSectionService;
import com.wxzz.elearing.course.service.ICurriculumSectionTopicService;
import com.wxzz.elearing.course.service.IExamServiceService;
import com.wxzz.elearning.common.vo.JgPageVo;
import com.wxzz.elearning.common.vo.ReturnVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/wx-curriculum-sectionTopic-controller")
@Api(tags = "课程章节测评试题")
public class WxCurriculumSectionTopicController {

    @Autowired
    private ICurriculumSectionTopicService curriculumSectionTopicService;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;
//
//    @ApiOperation(value = "根据章节id查询测评试题", notes = "根据章节id查询测评试题", httpMethod = "POST", tags = "小程序")
//    @RequestMapping(value = "listCurriculumSectionTopicByCurriculumSectionTopicId/{curriculumSectionTopicId}", method = RequestMethod.POST)
//    public ReturnVO<List<CurriculumSectionTopic>> listCurriculumSectionTopicByCurriculumSectionTopicId(@PathVariable Long curriculumSectionTopicId) {
//        return curriculumSectionTopicService.listCurriculumSectionTopicByCurriculumSectionTopicId(curriculumSectionTopicId,0);
//    }


    @ApiOperation(value = "章节测评结算分数", notes = "章节测评结算分数", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "curriculumSectionTopicIsPass", method = RequestMethod.POST)
    public ReturnVO<WxUserCurriculumSection> curriculumSectionTopicIsPass(@RequestHeader String token, @RequestBody CurriculumSectionTopicIsPassDto curriculumSectionTopicIsPassDto) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        return curriculumSectionTopicService.curriculumSectionTopicIsPass(jsonObject, curriculumSectionTopicIsPassDto);
    }

    @Autowired
    private IExamServiceService examServiceService;

    @ApiOperation(value = "在线考试测试完毕时调用", notes = "在线考试测试完毕时调用", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "examIsPass", method = RequestMethod.POST)
    public ReturnVO<WxUserExamHistory> examIsPass(@RequestHeader String token, @Valid @RequestBody ExamIsPassDto examIsPassDto) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        return examServiceService.examIsPass(jsonObject.getLong("id"), examIsPassDto);
    }

    @Autowired
    private ICurriculumSectionService curriculumSectionService;

    @ApiOperation(value = "列出历史章节测评记录", notes = "列出历史章节测评记录", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "listWxUserCurriculumHistory/{curriculumSectionId}", method = RequestMethod.POST)
    public ReturnVO<List<WxUserCurriculumSection>> listWxUserCurriculumHistory(@RequestHeader String token, @PathVariable Long curriculumSectionId) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        return curriculumSectionService.listWxUserCurriculumHistory(jsonObject.getLong("id"), curriculumSectionId);
    }
}
